package com.zhu.controller;

import com.zhu.pojo.Type;
import com.zhu.pojo.User;
import com.zhu.result.Result;
import com.zhu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.List;


@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @CrossOrigin //解决跨域问题
    @PostMapping(value = "/api/login")
    @ResponseBody //将java对象转为json格式的数据
    public Result login(@RequestBody User requestUser) {
        String username = requestUser.getUsername();
        username = HtmlUtils.htmlEscape(username);// HTML编码进行转义

        User user = userService.getByUsernameAndPassword(username,requestUser.getPassword());
        if(null == user) {
            return new Result(400);
        } else {
            return new Result(200);
        }
    }

    @CrossOrigin //解决跨域问题
    @GetMapping("/api/user/{username}")
    public int getUseIdByName(@PathVariable("username") String username) {
        return userService.getByName(username).getId();
    }

}


